package com.dk.assertion;

import com.alibaba.fastjson.JSON;
import com.dk.domain.ApiAutoTestEntity;
import com.dk.domain.assertion.CheckPointEntity;
import com.dk.enumerate.HttpEnum;
import com.dk.util.http.res.ResUtil;
import com.dk.util.log.Log;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 异常检查断言
 */
public class ErrorCheckLink extends AssertChain {
    /**
     * 异常断言检查
     * @param apiAutoTestEntity 检查点对象
     * @return
     */
    @Override
    public ApiAutoTestEntity assertBody(ApiAutoTestEntity apiAutoTestEntity) {
        //最终返回结果
        AtomicBoolean result = new AtomicBoolean(true);
        //获取检查点对象
        CheckPointEntity checkPointEntity = JSON.parseObject(apiAutoTestEntity.getCheckPoint(),CheckPointEntity.class);
        if(checkPointEntity.getErrorCheck()!=null&&checkPointEntity.getErrorCheck()){
            Log.info("---【用例断言】:开始异常检查断言");
           String headers = apiAutoTestEntity.getResHeaders();
           if(headers.contains(HttpEnum.CODE_400.getValue())||headers.contains(HttpEnum.CODE_401.getValue())||headers.contains(HttpEnum.CODE_402.getValue())||
                   headers.contains(HttpEnum.CODE_403.getValue())||headers.contains(HttpEnum.CODE_404.getValue())||headers.contains(HttpEnum.CODE_405.getValue())||
                   headers.contains(HttpEnum.CODE_406.getValue())||headers.contains(HttpEnum.CODE_407.getValue())||headers.contains(HttpEnum.CODE_500.getValue())||
                   headers.contains(HttpEnum.CODE_501.getValue())||headers.contains(HttpEnum.CODE_502.getValue())||headers.contains(HttpEnum.CODE_503.getValue())||
                   headers.contains(HttpEnum.CODE_504.getValue())||headers.contains(HttpEnum.CODE_505.getValue())){
                result.set(false);
                Log.error("---【用例断言】:断言失败,响应异常");
           }
            Log.info("---【用例断言】:异常检查断言结束");
        }
        apiAutoTestEntity.setAssertResult(result.get());
        return apiAutoTestEntity;
    }
}
